package com.agricultural.machine.factory;

import com.agricultural.machine.common.IMachine;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 农机工厂类 - 工厂方法模式
 */
public class MachineFactory {
    private static MachineFactory instance;
    private final Map<String, IMachine> machines;

    private MachineFactory() {
        this.machines = new HashMap<>();
    }

    public static synchronized MachineFactory getInstance() {
        if (instance == null) {
            instance = new MachineFactory();
        }
        return instance;
    }

    /**
     * 创建农机
     */
    public IMachine createMachine(String type) {
        IMachine machine;
        
        switch (type.toLowerCase()) {
            case "tractor":
                machine = new TractorMachine();
                break;
            case "harvester":
                machine = new HarvesterMachine();
                break;
            case "seeder":
                machine = new SeederMachine();
                break;
            case "sprayer":
                machine = new SprayerMachine();
                break;
            default:
                throw new IllegalArgumentException("不支持的农机类型: " + type);
        }
        
        machines.put(machine.getMachineId(), machine);
        return machine;
    }

    /**
     * 根据ID获取农机
     */
    public IMachine getMachine(String machineId) {
        return machines.get(machineId);
    }

    /**
     * 获取所有农机
     */
    public Map<String, IMachine> getAllMachines() {
        return new HashMap<>(machines);
    }

    /**
     * 获取所有农机列表
     */
    public List<IMachine> getAllMachinesList() {
        return new ArrayList<>(machines.values());
    }
}
















